import Vue from "vue";
import VueRouter from "vue-router";
import ShopView from "@/views/shop/ShopView.vue";
import MyLayout from "@/layout/MyLayout.vue";
import ShoppingCart from "@/views/shop/ShoppingCart.vue";
import MyOrder from "@/views/shop/MyOrder.vue";
import Login from "@/views/Login.vue";
import AddressManage from "@/views/shop/AddressManage.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/shop",
  },
  {
    path: "/demo",
    name: "demo",
    component: () => import("@/views/shop/demo.vue"),
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("@/views/Register.vue"),
  },
  {
    path: "/shop",
    component: () => import("@/layout/ShopLayout.vue"),
    children: [
      {
        path: "/",
        name: "shop",
        component: ShopView,
      },
      {
        path: "/shop/detail",
        name: "detail",
        component: () => import("@/views/shop/Detail.vue"),
      },
      {
        path: "/mine",
        component: MyLayout,
        children: [
          {
            path: "/",
            name: "profile",
            component: () => import("@/views/shop/ProfileView.vue"),
          },
          {
            path: "/cart",
            name: "ShoppingCart",
            component: ShoppingCart,
          },
          {
            path: "/myOrder",
            name: "myOrderView",
            component: MyOrder,
          },
          {
            path: "/customerServ",
            name: "customerService",
            component: () => import("@/views/shop/CustomerServ.vue"),
          },
          {
            path: "/addressManage",
            component: AddressManage,
          },
        ],
      },
    ],
  },
  {
    path: "/*",
    component: () => import("@/views/404.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

// 🛡️ 路由导航守卫：判断是否登录
// router.beforeEach((to, from, next) => {
//   const publicPages = ["/login", "/register", "/shop", "/shop/detail"];
//   const authRequired = !publicPages.includes(to.path);
//   const loggedIn = !!localStorage.getItem("user");

//   if (authRequired && !loggedIn) {
//     return next({ path: "/login", query: { redirect: to.fullPath } });
//   }

//   next();
// });

export default router;
